//
// Source code recreated from a .class file by IntelliJ IDEA
// (powered by FernFlower decompiler)
//

package com.ysy.tz.core.utils;

import com.sun.javafx.print.PrintHelper;
import com.sun.javafx.print.Units;
import javafx.print.PageLayout;
import javafx.print.PageOrientation;
import javafx.print.Paper;
import javafx.print.PrintResolution;
import javafx.print.Printer;
import javafx.print.PrinterJob;
import javafx.scene.Node;
import javafx.scene.Parent;
import javafx.scene.layout.Region;
import javafx.scene.transform.Scale;
import javafx.scene.web.WebView;

public class PrintUtils {
    public PrintUtils() {
    }

    public static void printNode(Parent parent, int paperWidth, int paperHeight, int dpi, int count) {
        int mm = (int)Math.ceil(0.03937007874015748 * (double)dpi);
        parent.prefWidth((double)(54 * mm));
        parent.prefHeight((double)(90 * mm));
        Printer printer = Printer.getDefaultPrinter();
        PrinterJob printerJob = PrinterJob.createPrinterJob();
        printerJob.getJobSettings().setCopies(count);
        Paper paper = PrintHelper.createPaper(paperWidth + "x" + paperHeight, (double)paperWidth, (double)paperHeight, Units.MM);
        PageLayout pageLayout = printer.createPageLayout(paper, PageOrientation.PORTRAIT, 0.0, 0.0, 0.0, 0.0);
        double width = 0.0;
        double height = 0.0;
        if (parent instanceof Region) {
            Region region = (Region)parent;
            region.applyCss();
            region.layout();
            width = region.getPrefWidth();
            height = region.getPrefHeight();
        } else if (parent instanceof Node) {
            parent.applyCss();
            width = (double)paperWidth;
            height = (double)paperHeight;
        }

        PrintResolution resolution = printerJob.getJobSettings().getPrintResolution();
        width /= (double)resolution.getFeedResolution();
        System.out.println("NEW WIDTH : " + resolution.getFeedResolution() + "," + width);
        height /= (double)resolution.getCrossFeedResolution();
        System.out.println("NEW HEIGHT : " + resolution.getCrossFeedResolution() + "," + height);
        double scaleX = pageLayout.getPrintableWidth() / width / (double)dpi;
        double scaleY = pageLayout.getPrintableHeight() / height / (double)dpi;
        System.out.println("scalex ===>>> " + scaleX + "," + scaleY);
        Scale scale = new Scale(scaleX, scaleY);
        if (scale != null) {
            parent.getTransforms().add(scale);
        } else {
            parent.getTransforms().add(new Scale((double)resolution.getFeedResolution() / width, (double)resolution.getCrossFeedResolution() / height));
        }

        if (printerJob != null) {
            boolean success = printerJob.printPage(pageLayout, parent);
            if (success) {
                printerJob.endJob();
            }
        }

    }

    public static void printWebView(WebView webView, int paperWidth, int paperHeight, Scale scale) {
        Printer printer = Printer.getDefaultPrinter();
        PrinterJob printerJob = PrinterJob.createPrinterJob();
        Paper paper = PrintHelper.createPaper(paperWidth + "x" + paperHeight, (double)paperWidth, (double)paperHeight, Units.POINT);
        printer.createPageLayout(paper, PageOrientation.PORTRAIT, 0.0, 0.0, 0.0, 0.0);
        double width = webView.getPrefWidth();
        double height = webView.getPrefHeight();
        PrintResolution resolution = printerJob.getJobSettings().getPrintResolution();
        if (scale != null) {
            webView.getTransforms().add(scale);
        } else {
            webView.getTransforms().add(new Scale((double)resolution.getFeedResolution() / width, (double)resolution.getCrossFeedResolution() / height));
        }

        if (printerJob != null) {
            webView.getEngine().print(printerJob);
            printerJob.endJob();
        }

    }
}
